package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @description /index访问请求交给控制类IndexController处理
 * @author Leon Plious
 * @date 2020年7月9日
 */

@Controller
public class IndexController{

	@RequestMapping("/index")
	public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
		// 试图模型为index.jsp
		ModelAndView mav = new ModelAndView("index");
		// 数据模型message，内容是Hello Spring MVC!
		mav.addObject("message", "Hello Spring MVC!");
		return mav;
	}
	
	@RequestMapping("/jump")
	public ModelAndView jump() throws Exception{
		ModelAndView modelAndView = new ModelAndView("redirect:/index");
		
		return modelAndView;
	}
	
	
	/**
	 * @description session访问次数统计
	 * @param session
	 * @return
	 * @date 2020年7月9日
	 */
	@RequestMapping("/check")
	public ModelAndView check(HttpSession session) {
		Integer i = (Integer) session.getAttribute("count");
		
		if(null == i)
			i=0;
		i++;
		
		session.setAttribute("count", i);
		ModelAndView mav = new ModelAndView("check");
		
		return mav;
	}

	
	@RequestMapping("clear")
	public ModelAndView clear(HttpSession session) {
		session.setAttribute("count", 0);
		
		ModelAndView modelAndView = new ModelAndView("redirect:/check");
		return modelAndView;
	}
}
